package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    // 新增菜品
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        Dish dish =new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish); // 插入 Dish 对象

        Long dishId = dish.getId ();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    // 菜品分页查询
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询：{}", dishPageQueryDTO);
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    // 根据id查询菜品
    @Override
    public DishVO getById(Long id) {
        log.info("根据id查询菜品：{}", id);
        Dish dish = dishMapper.getById(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;

    }

    //菜品修改
    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        log.info("菜品修改：{}", dishDTO);
        Dish dish =new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        Long dishId = dishDTO.getId ();
        dishFlavorMapper.deleteByDishId(dishId);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors){
            flavor.setDishId(dishId);
        }
        dishFlavorMapper.insertBatch(flavors);

    }

    // 根据分类id查询菜品
    @Override
    public DishVO list(Long categoryId) {
        log.info("根据分类id查询菜品：{}", categoryId);
        DishVO dishVO = dishMapper.list(categoryId);
        if(dishVO != null){
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(dishVO.getId());
            dishVO.setFlavors(flavors);
            return dishVO;
        }
        return null;
    }

    // 菜品批量删除
    @Override
    @Transactional
    public void deleteById(List<Long> ids) {
        // 判断当前菜品是否能够删除（是否在售）
        for(Long id : ids){
            Dish dish = dishMapper.getById(id);
            Integer status = dish.getStatus();
            if (status == StatusConstant.ENABLE){
                //如果当前菜品正在售卖，则不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //被删除的菜品中，是否有关联套餐
        List<Long> setmealIds = setmealMapper.getSetmealIdsByDishIds(ids);
        //判断当前菜品是否关联了套餐，如果关联了，则不能删除
        if(setmealIds != null && setmealIds.size() > 0){
            //当前菜品有套餐关联，则不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品口味关联数据
        for(Long id : ids){
            dishMapper.deleteByDishId(id);// 删除菜品数据
            dishFlavorMapper.deleteByDishId(id);// 删除菜品口味数据
        }

    }

    // 菜品起售停售
    @Override
    public void startOrStop(Integer status, Long id) {
        dishMapper.startOrStop(status,id);
    }


}
